<?
$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp;
		switch($gOp)
		{
			case "shwModPass":
				$this->shwModPass();
				break;
			case "shwListAdmin":
				$this->shwListAdmin();
				break;
			case "shwListRole":
				$this->shwListRole();
				break;
			case "shwListAuth":
				$this->shwListAuth();
				break;
		    case "savePass":
		        $this->savePass();
		        break;
		    case "ajaxGetMenus":
				$this->ajaxGetMenus();
				break;
		    case "ajaxSaveRole":
				$this->ajaxSaveRole();
				break;
		    case "ajaxSaveUser":
		        $this->ajaxSaveUser();
		        break;
		    case "ajaxDelRole":
				$this->ajaxDelRole();
				break;
		    case "ajaxDelAdmin":
				$this->ajaxDelAdmin();
				break;
		    case "ajaxBanAdmin":
				$this->ajaxBanAdmin();
				break;
		    case "ajaxGetRoles":
		        $this->ajaxGetRoles();
		        break;
			default:
				$this->shwModPass();
				break;
		}
	}
	function ajaxBanAdmin()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$id = $INCOME['id'];
		$ban = $INCOME['ban'];
		if($id==1)
		{
			$data = array("st"=>"err","msg"=>"该用户为默认超级管理员，不允许该操作");
		}
		else
		{
			$DB->db_query("update admin_info set isInvalid=".$ban." where id=$id");
			$data = array("st"=>"ok","msg"=>"设置成功");
			echo $GJson->encode($data);
		}
	}
	function ajaxDelAdmin()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$id = $INCOME['id'];
		if($id==1)
		{
			$data = array("st"=>"err","msg"=>"该用户为默认超级管理员，不允许删除");
		}
		else
		{
			$DB->db_query("delete from admin_info where id=$id");
			$data = array("st"=>"ok","msg"=>"删除成功");
		}
		echo $GJson->encode($data);
	}
	function ajaxDelRole()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$id = $INCOME['id'];
		if($id==1)
		{
			$data = array("st"=>"err","msg"=>"该角色为默认角色，不允许删除");
		}
		else
		{
			$r = $DB->db_query_first("SELECT * FROM admin_info where roleId=$id");
			if($r)
			{
				$data = array("st"=>"err","msg"=>"该角色下存在用户，不允许删除");
			}
			else
			{
				$DB->db_query("delete from admin_role where id=$id");
				$data = array("st"=>"ok","msg"=>"删除成功");
			}
		}
		echo $GJson->encode($data);
	}
	function ajaxSaveUser()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$name = $INCOME['name'];
		$pass = $INCOME['pass'];
		$id = $INCOME['id'];
		$roleId=$INCOME['roleId'];
		$roleName=$INCOME['roleName'];
		$roleRelId = $INCOME['roleRelId'];
		$roleRelName = $INCOME['roleRelName'];
		$data = array();
		if($name)
		{
			if($id)
			{
					//$fields['name'] = $name;
					$r = $DB->db_query_first("select * from admin_info where name='$name' and id!=$id");
					if($r)
					{
						$data = array("st"=>"err1","msg"=>"已经存在");
					}
					else
					{
						$fields['name'] = $name;
						$fields['pass'] = md5($pass);
						$fields['editTime'] = $gCurTime;
						$fields['roleId'] = $roleId;
						$fields['roleName'] = $roleName;
						$fields['roleRelId'] = $roleRelId;
						$fields['roleRelName'] = $roleRelName;
						$DB->db_form_update("admin_info",$fields,"id=$id");
						$data = array("st"=>"ok","msg"=>"保存成功");
					}
			}
			else
			{
				$r = $DB->db_query_first("select * from admin_info where name='$name'");
				if($r)
				{
					$data = array("st"=>"err1","msg"=>"已经存在");
				}
				else
				{
					$fields['name'] = $name;
					$fields['pass'] = md5($pass);
					$fields['addTime'] = $gCurTime;
					$fields['roleId'] = $roleId;
					$fields['roleName'] = $roleName;
					$fields['roleRelId'] = $roleRelId;
					$fields['roleRelName'] = $roleRelName;
					$DB->db_form_insert("admin_info",$fields);
					$data = array("st"=>"ok","msg"=>"保存成功");
				}
			}
		}
		else
		{
			$data = array("st"=>"err2","msg"=>"名称不能为空");
		}
		echo $GJson->encode($data);
	}
	function ajaxSaveRole()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$selMenus = $INCOME['selMenus'];
		$name = $INCOME['name'];
		$id = $INCOME['id'];
		if($name)
		{
			if($id)
			{
				$r = $DB->db_query_first("select * from admin_role where name='$name' and id!=$id");
				if($r)
				{
					$data = array("st"=>"err1","msg"=>"已经存在");
				}
				else
				{
					if(is_array($selMenus))
					{
						$selMenus = implode(",",$selMenus);
					}
					$fields['name'] = $name;
					$fields['editTime'] = $gCurTime;
					$fields['auth'] = $selMenus;
					$DB->db_form_update("admin_role",$fields,"id=$id");
					$data = array("st"=>"ok","msg"=>"保存成功");
				}
			}
			else
			{
				$r = $DB->db_query_first("select * from admin_role where name='$name'");
				if($r)
				{
					$data = array("st"=>"err1","msg"=>"已经存在");
				}
				else
				{
					if(is_array($selMenus))
					{
						$selMenus = implode(",",$selMenus);
					}
					$fields['name'] = $name;
					$fields['addTime'] = $gCurTime;
					$fields['auth'] = $selMenus;
					$DB->db_form_insert("admin_role",$fields);
					$data = array("st"=>"ok","msg"=>"保存成功");
				}
			}
		}
		else
		{
			$data = array("st"=>"err2","msg"=>"名称不能为空");
		}
		echo $GJson->encode($data);
	}
	function ajaxGetRoles()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$CONF,$GJson,$DB;
	    $id = $INCOME['id'];
	    $DB->db_query("select * from admin_role");
	    $data = array();
	    while($r=$DB->db_fetch_row())
	    {
	    	$isSel = 0;
	    	if($r['id']==$id)
	    	{
	    		$isSel = 1;
	    	}
	    	$data[] = array("id"=>$r['id'],"name"=>$r['name'],"isSel"=>$isSel);
	    }
		echo $GJson->encode($data);
	}
	function ajaxGetMenus()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$CONF,$GJson,$DB;
		$id = $INCOME['id'];
		$menus = $CONF['menus'];
		if($id)
		{
			$r = $DB->db_query_first("select * from admin_role where id=".$id);
			$haveAuth = explode(",",$r['auth']);
			$newMenu  = array();
			foreach($CONF['menus'] as $v)
			{
				if(in_array($v['id'],$haveAuth))
				{
					$v['isSel'] = 1;
				}
				else
				{
					$v['isSel'] = 0;
				}
				$subMenus = array();
				foreach($v['menus'] as $v1)
				{
					if(in_array($v1['id'],$haveAuth))
					{
						$v1['isSel'] = 1;
					}
					else
					{
						$v1['isSel'] = 0;
					}
					$subMenus[] = $v1;
				}
				$v['menus'] = $subMenus;
				$newMenu[] = $v;
			}
			$menus = $newMenu;
		}
		echo $GJson->encode($menus);
	}
	function shwListAdmin()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB;
		$sql = "select * from admin_info";
		$DB->db_query($sql);
		$data = null;
		$idx = 0;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
		    if($r['roleRelName'])
		    {
		    	$r['roleName'] = $r['roleName']."---".$r['roleRelName'];
		    }
		    $data[] = array("disId"=>$idx,"id"=>$r['id'],"name"=>$r['name'],"role"=>$r['roleName'],"roleId"=>$r['roleId'],"roleRelId"=>$r['roleRelId'],"isInvalid"=>$r['isInvalid']);	
		}
		$gSmarty->assign("data",$data);
		$gSmarty->display("admin/admin.tpl");
	}
	function shwListAuth()
	{
		global $gSmarty,$gAct,$gOp,$INCOME;
		$gSmarty->display("admin/auth.tpl");
	}
	function shwListRole()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB;
		$sql = "select * from admin_role";
		$DB->db_query($sql);
		$data = null;
		$idx = 0;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
		    $data[] = array("disId"=>$idx,"id"=>$r['id'],"name"=>$r['name']);	
		}
		$gSmarty->assign("data",$data);
		$gSmarty->display("admin/role.tpl");
	}
	function savePass()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$_SESSION,$gWeb,$DB;
		$oldPass = $INCOME['oldPass'];
		$newPass = $INCOME['newPass'];
		$conPass = $INCOME['conPass'];
		$gCurUrl = "?act=$gAct&op=shwModPass";
		if($oldPass && $newPass && $conPass)
		{
			if($newPass!=$conPass)
			{
				$gWeb->showMsg("输入的新密码与确认密码不一样",$gCurUrl);
				exit;
			}
			if($_SESSION['adminUser']['pass']==md5($oldPass))
			{
				$DB->db_query("update admin_info set pass='".md5($newPass)."' where id=".$_SESSION['adminUser']['id']);
				$_SESSION['adminUser']['pass']=md5($newPass);
				$gWeb->showMsg("修改成功",$gCurUrl);
			}
			else
			{
				$gWeb->showMsg("输入的原密码不正确",$gCurUrl);
			}
		}
	}
	function shwModPass()
	{
		global $gSmarty,$gAct,$gOp,$INCOME;
		$gSmarty->display("admin/pass.tpl");
	}
	 
}
?>